package com.shwread.httpsdk.util;

import java.util.List;


/**
 * @Title: Util.java
 * @Package com.surfingread.httpsdk.util
 * @Description: TODO(添加描述)
 * @author 刘章
 * @date 2014年1月22日 下午2:04:09
 */
public class Util
{
	/**
	 * @Title: EmptyToNUll
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param value
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 * @date 2012-11-13 上午11:40:34
	 */
	public static String EmptyToNUll(String value) {
		if (isEmpty(value))
		{
			value = null;
		}
		return value;
	}
	
	/**
	 * @Title: NUllToString
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 * @date 2014年1月22日 下午2:52:58
	 */
	public static String NUllToString(String value) {
		if (isEmpty(value))
		{
			value = "";
		}
		return value;
	}
	
	/**
	 * @Title: reverse
	 * @Description:String反转
	 * @param @param value
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 * @date 2012-11-7 下午2:10:10
	 */
	public static String reverse(String value) {
		StringBuffer sb = new StringBuffer(value);
		sb = sb.reverse();
		return sb.toString();
	}
	
	/**
	 * 判断String为空
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isEmpty(String string) {
		return (string == null || "".equalsIgnoreCase(string.trim())||"null".equalsIgnoreCase(string.trim()));
	}
	
	/**
	 * 判断String不为空
	 * 
	 * @param string
	 * @return
	 */
	public static boolean isNotEmpty(String string) {
		return string != null && string.trim().length() > 0;
	}
	
	/**
	 * 章节内容
	 * 
	 * @param xml
	 * @return
	 */
	public static String doBookChapterInfo(String title, String xml) {
		String chapter = "";
		String head = "";
		String contentXml = xml.substring(xml.lastIndexOf("[CDATA[") + 7, xml.lastIndexOf("]]") - 2);
		String body = SimpleXmlHelper.getTagValue(contentXml, "body");
		if (contentXml.contains("<p>"))
		{
			head = contentXml.substring(0, contentXml.indexOf("<p>"));
		}
		else
		{
			head = contentXml.substring(0, contentXml.indexOf("<P>"));
		}
		//组装内容
		chapter = head + "<h1>" + title + "</h1>" + body + "</body></html>";
		
		return chapter;
	}
	
	/**
	 * @param contentName
	 * @param author
	 * @param coverid
	 * @param xml
	 * @return
	 */
	public static String tocNcx(String contentName, String author, String coverid, String contentType, String xml) {
		StringBuffer toc = new StringBuffer();
		toc.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		toc.append("<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">\n");
		toc.append("<head>\n");
		toc.append("<meta content=\"0\" name=\"dtb:maxPageNumber\"/>\n");
		toc.append("<meta content=\"0\" name=\"dtb:totalPageCount\"/>\n");
		toc.append("<meta content=\"-1\" name=\"dtb:uid\"/>\n");
		toc.append("<meta content=\"1\" name=\"dtb:depth\"/>\n");
		toc.append("</head>\n");
		toc.append("<docTitle>\n");
		toc.append("<text>" + contentName + "</text>\n");
		toc.append("</docTitle>\n");
		toc.append("<docAuthor>\n");
		toc.append("<text>" + author + "</text>\n");
		toc.append("</docAuthor>\n");
		toc.append("<doCover>\n");
		toc.append("<text>" + coverid + "</text>\n");
		toc.append("</doCover>\n");
		toc.append("<navMap>\n");
		toc.append(formateCatalogue(contentType, xml));
		toc.append("</navMap>\n");
		toc.append("</ncx>\n");
		//#debug
		System.out.println(toc.toString());
		return toc.toString();
	}
	
	/**
	 * 创建目录信息
	 * 
	 * @param xml
	 * @return
	 */
	private static String formateCatalogue(String contentType, String xml) {
		List<String> VolumnInfo = SimpleXmlHelper.getTagDataList(xml, "VolumnInfo");
		String toc = "";
		if ("3".equals(contentType))
		{
			for (String volumnXml : VolumnInfo)
			{
				String volumnID = SimpleXmlHelper.getTagValue(volumnXml, "volumnID");
				String volumnName = SimpleXmlHelper.getTagValue(volumnXml, "volumnName");
				String order = SimpleXmlHelper.getTagValue(volumnXml, "order");
				toc += navPoint(volumnID, order, volumnName, volumnID + ".html");
			}
		}
		else
		{
			for (int i = 0; i < VolumnInfo.size(); i++)
			{
				String chapter = "卷" + (i + 1);
				List<String> chapterInfoXml = SimpleXmlHelper.getTagDataList(VolumnInfo.get(i), "ChapterInfo");
				for (String chapterInfo : chapterInfoXml)
				{
					String chapterID = SimpleXmlHelper.getTagValue(chapterInfo, "chapterID");
					String name = SimpleXmlHelper.getTagValue(chapterInfo, "chapterName");
					String order = SimpleXmlHelper.getTagValue(chapterInfo, "order");
					String chapterName = chapter + " " + name;
					toc += navPoint(chapterID, order, chapterName, chapterID + ".html");
				}
			}
		}
		return toc;
	}
	
	/**
	 * 创建章节信息
	 * 
	 * @param chapterid
	 * @param order
	 * @param title
	 * @param content
	 * @return
	 */
	private static String navPoint(String chapterid, String order, String title, String content) {
		
		StringBuffer navPoint = new StringBuffer();
		navPoint.append("<navPoint id=\"" + chapterid + "\" playOrder=\"" + order + "\">\n");
		navPoint.append("<navLabel>\n<text>" + title + "</text>\n</navLabel>\n");
		navPoint.append("<content src=\"" + content + "\"/>\n");
		navPoint.append("</navPoint>\n");
		return navPoint.toString();
	}
	
}
